package edu.jhu.cs.oose.scheduleit.vo;

import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import edu.jhu.cs.oose.scheduleit.util.FilterParser;
import edu.jhu.cs.oose.scheduleit.util.JoinRule;

/**
 * Entity class of Group
 * @author oose_group2
 *
 */
@Entity
@Table(name ="Team")
public class Group implements Comparable<Group>{
	
	/**
	 * The identifier of group
	 */
	@Id
	@GeneratedValue
	private int id;
	
	/**
	 * The topic of this group
	 */
	@Basic
	@Column(length = 2048)
	private String topic;
	
	/**
	 * The detailed information of this group
	 */
	@Basic
	private String description;
	
	/**
	 * The tags of this group
	 */
	@Basic
	@Column(length = 2048)
	private String tags;
	
	/**
	 * How many times this group has been clicked
	 */
	@Basic
	private long clicks;
	
	/**
	 * The joined rule of this group
	 */
	@Enumerated(EnumType.STRING)
	private JoinRule joinRule;

	/**
	 * The host of this group
	 */
	@OneToOne
	private UserInfo host;
	
	/**
	 * The shared schedule of this schedule
	 */
	@OneToOne
	private Schedule schedule;
	
		
	/**
	 * The users who have submitted the application for this group but haven't been confirmed
	 */
	@SuppressWarnings("unused")
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="Team_appliedUsers")	
	private List<UserInfo> appliedUsers;
	
	/**
	 * The users who have joined this group
	 */
	@SuppressWarnings("unused")
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="Team_joinedUsers")
	private List<UserInfo> joinedUsers;
	
	/**
	 * The users who have been denied to join this group
	 */
	@SuppressWarnings("unused")
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="Team_deniedUsers")	
	private List<UserInfo> deniedUsers;
	
	/**
	 * The getter of id
	 * @return
	 */
	public int getId() {
		return id;
	}

	/**
	 * The setter of id
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * The getter of host
	 * @return
	 */
	public UserInfo getHost() {
		return host;
	}

	/**
	 * The setter of host
	 * @param host
	 */
	public void setHost(UserInfo host) {
		this.host = host;
	}

	/**
	 * The getter of schedule
	 * @return
	 */
	public Schedule getSchedule() {
		return schedule;
	}

	/**
	 * The setter of schedule
	 * @param schedule
	 */
	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}
	

	/**
	 * The getter of topic
	 */
	public String getTopic() {
		return FilterParser.translateBeforeTopicRead(this.topic);
	}

	/**
	 * The setter of topic
	 * @param topic
	 */
	public void setTopic(String topic) {
		this.topic = FilterParser.translateBeforeTopicWrite(topic);
	}

	/**
	 * The getter of clicks
	 * @return
	 */
	public long getClicks() {
		return this.clicks;
	}

	/**
	 * The setter of clicks
	 * @param clicks
	 */
	public void setClicks(long clicks) {
		this.clicks = clicks;
	}

	/**
	 * The getter of joinedRule
	 * @return
	 */
	public JoinRule getJoinRule() {
		return this.joinRule;
	}

	/**
	 * The setter of joinedRule
	 * @param joinRule
	 */
	public void setJoinRule(JoinRule joinRule) {
		this.joinRule = joinRule;
	}
	
	/**
	 * The getter of tags of this group
	 * @return
	 */
	public List<String> getTags(){
		return FilterParser.translateBeforeTagRead(this.tags);
	}
	
	public void setTags(List<String> tags) {
		this.tags = FilterParser.translateBeforeTagWrite(tags);
	}
	
	
	/**
	 * The getter of description
	 * @return
	 */
	public String getDescription() {
		return this.description != null? this.description: "";
	}

	/**
	 * The setter of description
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Override
	public int compareTo(Group group) {
		if (this.getId() == group.getId()){
			return 0;
		}else if (this.getId() == 0){
			return -1;
		}else if (group.getId() == 0){
			return 1;
		}else {
			return this.getId() > group.getId()? 1: -1;
		}
	}
	
	
}
